package com.smrs.asn.webapp.action;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.jof.framework.util.Pager;
import com.smrs.asn.enums.AsnStatusEnum;
import com.smrs.asn.model.AsnHeaderModel;
import com.smrs.asn.model.AsnHeaderSearchModel;
import com.smrs.basicdata.model.RegionModel;
import com.smrs.basicdata.model.StoreModel;
import com.smrs.goods.model.SupplierModel;
import com.smrs.security.model.UserModel;
import com.smrs.util.AppUtil;



public class AsnHeaderAction extends BaseAsnAction{
	private static final long serialVersionUID = 1L;
	private AsnHeaderModel asnHeader ;
	
	protected AsnHeaderSearchModel searchModel;
	protected Pager<AsnHeaderModel> pager = new Pager<AsnHeaderModel>(); 
	protected String titleName="入库";
	protected RegionModel region;
	protected StoreModel store;
	protected String buttonTitle;
	protected String asnHeaderIds;
	
	public String getAsnHeaderIds() {
		return asnHeaderIds;
	}

	public void setAsnHeaderIds(String asnHeaderIds) {
		this.asnHeaderIds = asnHeaderIds;
	}

	public String getButtonTitle() {
		return buttonTitle;
	}

	public void setButtonTitle(String buttonTitle) {
		this.buttonTitle = buttonTitle;
	}

	public AsnHeaderSearchModel getSearchModel() {
		return searchModel;
	}

	public void setSearchModel(AsnHeaderSearchModel searchModel) {
		this.searchModel = searchModel;
	}

	public StoreModel getStore() {
		return store;
	}

	public void setStore(StoreModel store) {
		this.store = store;
	}

	public RegionModel getRegion() {
		return region;
	}

	public void setRegion(RegionModel region) {
		this.region = region;
	}

	protected void prepareList(){
		supplierList = supplierService.getAllActive();
		storeList = storeService.getAllActive();
		regionList = regionService.getAllActive();		
	}
	
	public String addAsnHeader(){
		prepareList();
		
		if(StringUtils.isEmpty(actionCommand)){
			//supplierList = supplierService.getAllActive();
			//storeList = storeService.getAllActive();
			return SUCCESS;
		}
		SupplierModel supplier = this.supplierService.getByPK(asnHeader.getSupplierId());
		asnHeader.setSupplierCode(supplier.getCode());
		asnHeader.setSupplierName(supplier.getName());
		//asnHeader.setName(supplier.getName());
		store = this.storeService.getByPK(asnHeader.getStoreId());
		asnHeader.setStoreCode(store.getCode());
		asnHeader.setStoreName(store.getName());
		asnHeader.setSeqNo(asnSequenceService.updateAsnSequenceValue());
		asnHeader.setStatus(AsnStatusEnum.FRESH.getId());
		region = regionService.getByPK(asnHeader.getRegion().getId());
		//System.out.println("status="+AsnStatusEnum.toEnum('F').name());
		
		UserModel userModel = this.getUserModelFromSession();
		AppUtil.setCreateUserInfo(userModel, asnHeader);
		this.asnHeaderService.addModel(asnHeader);		
		this.addActionMessage("创建入库单成功");
		return "toUpdateAsnHeader";
	}
	
	
	public String searchAsnHeader(){
		prepareList();
		
		if(searchModel==null){
			searchModel = new AsnHeaderSearchModel();
		}
		//ActionCommandEnum action = ActionCommandEnum.toEnum(actionCommand);

		pager=asnHeaderService.findAsnHeader(searchModel,pager);
		this.actionCommand="query";
		return SUCCESS;
	}
	

	protected void prepareAuditStatus(){
		this.asnStatusList =new ArrayList<AsnStatusEnum>();
		//asnStatusList.add(AsnStatusEnum.ALL);
		asnStatusList.add(AsnStatusEnum.FRESH);
		asnStatusList.add(AsnStatusEnum.AUDITED);		
	}
	
	protected void prepareAccountStatus(){
		this.asnStatusList =new ArrayList<AsnStatusEnum>();
		//asnStatusList.add(AsnStatusEnum.ALL);
		asnStatusList.add(AsnStatusEnum.AUDITED);
		asnStatusList.add(AsnStatusEnum.ACCOUNTED);		
	}
	
	public String auditAsnHeader(){
		prepareList();
		prepareAuditStatus();
		if(searchModel==null){
			searchModel = new AsnHeaderSearchModel();
		}
		pager=asnHeaderService.findAsnHeader(searchModel,pager);
		this.actionCommand="audit";
		return SUCCESS;
	}
	
	public String accountAsnHeader(){
		prepareList();
		prepareAccountStatus();
		if(searchModel==null){
			searchModel = new AsnHeaderSearchModel();
		}
		pager=asnHeaderService.findAsnHeader(searchModel,pager);
		this.actionCommand="account";
		return SUCCESS;
	}
	
	public String addAsnHeaderInit(){

		return SUCCESS;
	}
	
	public String updateAsnHeader(){
		this.asnHeader = asnHeaderService.getByPK(asnHeader.getId());
		return SUCCESS;
	}
	
	protected List<Long> parseAsnHeaderIds(String asnHeaderIds){
		List<Long> listIds = new ArrayList<Long>();
		String[] ids = asnHeaderIds.split(",");
		for(int index=0;index<ids.length;index++){
			Long id = Long.parseLong(ids[index]);
			listIds.add(id);
		}
		return listIds;
	}
	
	public String deleteAsnHeader(){
		asnHeaderService.deleteModel(asnHeader);
		return "search";
	}
	
	
	public AsnHeaderModel getAsnHeader() {
		return asnHeader;
	}

	public void setAsnHeader(AsnHeaderModel asnHeader) {
		this.asnHeader = asnHeader;
	}
	
	public Pager<AsnHeaderModel> getPager() {
		return pager;
	}


	public void setPager(Pager<AsnHeaderModel> pager) {
		this.pager = pager;
	}

	public String getTitleName() {
		return titleName;
	}


	public void setTitleName(String titleName) {
		this.titleName = titleName;
	}
}
